package org.budo.mongo.jdbc.driver.statement.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;

import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.resultset.impl.MapArrayResultSet;
import org.budo.mongo.jdbc.driver.executor.MongoStatementExecutor;
import org.budo.mongo.jdbc.driver.statement.AbstractMongoStatement;

/**
 * 使用一个临时的 Collection 存储查询结果,使用后自动删除
 * 
 * @author lmw
 */
public class MongoEvalFindStatement extends AbstractMongoStatement {
    /**
     * @see org.budo.mongo.jdbc.driver.parser.jsqlparser.JSqlParserMongoStatementParser#mongoEvalFindStatement
     */
    public void execute(PreparedStatement preparedStatement) throws SQLException {
        String statementScript = this.statementJson(preparedStatement);

        MongoStatementExecutor statementExecutor = this.statementExecutor(preparedStatement);
        List<?> result = statementExecutor.eval(statementScript);

        ResultSetMetaData metaData = null;
        MapArrayResultSet resultSet = new MapArrayResultSet(metaData, result.toArray());

        ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}